package cn.wangqi.personal.controller;

import cn.wangqi.personal.common.BaseController;
import cn.wangqi.personal.domain.Collage;
import cn.wangqi.personal.service.ICollageService;
import cn.wangqi.personal.util.Page;
import cn.wangqi.personal.util.PageResultModel;
import cn.wangqi.personal.util.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * Created by admin on 2017/9/7.
 */
@Controller
@RequestMapping("collage")
public class CollageController extends BaseController{

    Logger logger = Logger.getLogger(CollageController.class);

    @Resource
    public ICollageService collageService;

    @RequestMapping("index.html")
    public String user(Model model){
        model.addAttribute("code","collage");
        return "collage/index";
    }
    @RequestMapping("findCollageList.html")
    @ResponseBody
    public String findCollageList(Integer currentPage){
        Page page = null;
        if(currentPage != null && currentPage >= 1){
            page = new Page();
            page.setPageSize(10);
            page.setStart((currentPage - 1) * 10);
        }
        List<Collage> collageList = collageService.listCollage(page);
        int count = collageService.countCollage();
        PageResultModel<Collage> result = new PageResultModel();
        result.setPageNo(currentPage!=null?currentPage:1);
        result.setPageSize(page != null ?page.getPageSize():1);
        result.setTotal(count);
        result.setDataList(collageList);
        return buildSuccessResponse((Serializable) result);
    }

    @RequestMapping("saveCollage.html")
    @ResponseBody
    public String saveCollage(Collage collage){
        try{
            if(collage==null){
                return this.buildErrorResponse("请输入信息");
            }
            if(collage.getCollageId()==null){
                return this.buildErrorResponse("请输入学院编号");
            }
            if(StringUtil.isEmpty(collage.getCollageName())){
                return this.buildErrorResponse("请输入学院名称");
            }
            if(StringUtil.isEmpty(collage.getAddress())){
                return this.buildErrorResponse("请输入办公地址");
            }
            collageService.saveCollage(collage);
            return buildSuccessResponse();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return this.buildErrorResponse(e.getMessage());
        }
    }
    @RequestMapping("deleteCollageById.html")
    @ResponseBody
    public String deleteCollageById(Integer id){
        try{
            if(id==null||id==0){
                return this.buildErrorResponse("删除失败");
            }
            collageService.deleteCollageById(id);
            return buildSuccessResponse();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return this.buildErrorResponse(e.getMessage());
        }
    }
    @RequestMapping("findCollageById.html")
    @ResponseBody
    public String findCollageById(Integer id){
        try{
            if(id==null||id==0){
                return this.buildErrorResponse("id为空");
            }
            Collage collage = collageService.findCollageById(id);
            return buildSuccessResponse(collage);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return this.buildErrorResponse(e.getMessage());
        }
    }
    @RequestMapping("updateCollage.html")
    @ResponseBody
    public String updateCollage(Collage collage){
        try{
            if(collage.getId()==null){
                return this.buildErrorResponse("Id为空");
            }
            if(collage==null){
                return this.buildErrorResponse("请输入信息");
            }
            if(collage.getCollageId()==null){
                return this.buildErrorResponse("请输入学院编号");
            }
            if(StringUtil.isEmpty(collage.getCollageName())){
                return this.buildErrorResponse("请输入学院名称");
            }
            if(StringUtil.isEmpty(collage.getAddress())){
                return this.buildErrorResponse("请输入办公地址");
            }
            collageService.updateCollage(collage);
            return buildSuccessResponse();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return this.buildErrorResponse(e.getMessage());
        }
    }
}
